/*
 ============================================================================
 Name		: FEFile.cpp
 Author	  : YangYu
 Version	 : 1.0
 Copyright   : this class for file operation include
 rename a file/copy a file/move a file/delete a file
 /get properties for a file
 Description : CFEEile implementation
 ============================================================================
 */

#include "FEFile.h"
#include <e32const.h>
class TEntry;

/**********************************************************************************
 * @Name:       ConstructL 
 * @Author:     YangYu
 * @Purpose:    New a CFileMan Object
 * @Returns:    void
 * @Parameters: 
 *********************************************************************************/

void CFEFile::ConstructL()
{
	iFm = CFileMan::NewL(iFs);
}

/**********************************************************************************
 * @Name:       NewLC 
 * @Author:     YangYu
 * @Purpose:    Two-phased constructor
 * 				Allocate memory for CFEFile object.
 * @Returns:    void
 * @Parameters: 
 *********************************************************************************/
CFEFile* CFEFile::NewLC()
{
	CFEFile* self = new (ELeave) CFEFile();
	CleanupStack::PushL(self);
	self->ConstructL();
	return self;
}

/**********************************************************************************
 * @Name:       NewL 
 * @Author:     YangYu
 * @Purpose:    Two-phased constructor
 * @Returns:    void
 * @Parameters: 
 *********************************************************************************/
CFEFile* CFEFile::NewL()
{
	CFEFile* self = CFEFile::NewLC();
	CleanupStack::Pop();
	return self;
}

/**********************************************************************************
 * @Name:       CFEFile
 * @Author:     YangYu
 * @Purpose:    Connect a file session
 * @Returns:    void
 * @Parameters: 
 *********************************************************************************/
CFEFile::CFEFile() :
	iFs(CEikonEnv::Static()->FsSession())
{

}

/**********************************************************************************
 * @Name:       ~CFEFile 
 * @Author:     YangYu
 * @Purpose:    Desconstructor
 * 				Release memory .
 * @Returns:    void
 * @Parameters: 
 *********************************************************************************/
CFEFile::~CFEFile()
{
	if (iFm)
	{
		delete iFm;
		iFm = NULL;
	}
}

/**********************************************************************************
 * @Name:       Rename
 * @Author:     YangYu
 * @Purpose:    Rename a file
 * @Returns:    ETrue if success,else EFalse
 * @Parameters: 
 * 				aFnOld--Path specifying the file(s) to be renamed. 
 * 				aFnNew--Path specifying the new name for the files . 
 * 						Any directories specified in this path that do not exist, 
 * 						will be created. 
 *********************************************************************************/
TInt CFEFile::Rename(TFileName aFnOld, TFileName aFnNew)
{
	TInt ret;
	ret = iFm->Rename(aFnOld, aFnNew, CFileMan::EOverWrite);
	if (ret != KErrNone)
		return EFalse;
	else
		return ETrue;
}

/**********************************************************************************
 * @Name:       Rename
 * @Author:     YangYu
 * @Purpose:    Delete a file
 * @Returns:    ETrue if success,else EFalse
 * @Parameters: 
 * 				aFnDelPath--Path indicating the file to be deleted. 
 *********************************************************************************/

TInt CFEFile::Delete(TFileName aFnDelPath)
{
	TInt ret;
	ret = iFm->Delete(aFnDelPath, CFileMan::EOverWrite);
	if (ret != KErrNone)
		return EFalse;
	else
		return ETrue;
}

/**********************************************************************************
 * @Name:       Rename
 * @Author:     YangYu
 * @Purpose:    Copy a file
 * @Returns:    ETrue if success,else EFalse
 * @Parameters: 
 * 				aFnCopyOld--Path indicating the file to be copied. 
 * 				aFnCopyNew--Path(and optionally the filename)indicating the file 
 * 							into which the file is to be copied. 
 *********************************************************************************/
TInt CFEFile::Copy(TFileName aFnCopyOld, TFileName aFnCopyNew)
{
	TInt ret;
	ret = iFm->Copy(aFnCopyOld, aFnCopyNew, CFileMan::EOverWrite);
	if (ret != KErrNone)
		return EFalse;
	else
		return ETrue;
}

/**********************************************************************************
 * @Name:       Move
 * @Author:     YangYu
 * @Purpose:    Move a file
 * @Returns:    ETrue if success,else EFalse
 * @Parameters: 
 * 				aFnMoveOld--Path indicating the file to be moved. 
 * 				aFnMoveNew--Path(and optionally the filename)indicating the file 
 * 							into which the file is to be moved. 
 *********************************************************************************/
TInt CFEFile::Move(TFileName aFnMoveOld, TFileName aFnMoveNew)
{
	TInt ret;
	ret = iFm->Move(aFnMoveOld, aFnMoveNew, CFileMan::EOverWrite);
	if (ret != KErrNone)
		return EFalse;
	else
		return ETrue;
}

/**********************************************************************************
 * @Name:       LoadFileProperties
 * @Author:     YangYu
 * @Purpose:    Get the Properties from a file
 * @Returns:    ETrue if success,else EFalse
 * @Parameters: 
 * 				aFileName--Path indicating the file to be loaded properties  
 * 				aProperties--A TDes refer include the information of aFileName
 **********************************************************************************/

void CFEFile::LoadFileProperties(TDesC& aFileName, TDes& aProperties)
{
	TEntry entry;
	TInt err = iFs.Entry(aFileName, entry);
	if (err == KErrNone)
	{
		if (entry.IsHidden())
		{
			aProperties.Append(_L("Hidden File\n"));
		}
		else
		{
			aProperties.Append(_L("Not Hidden File\n"));
		}

		if (entry.IsReadOnly())
		{
			aProperties.Append(_L("ReadOnly File\n"));
		}
		else
		{
			aProperties.Append(_L("Not ReadOnly File\n"));
		}

		if (entry.IsSystem())
		{
			aProperties.Append(_L("System File\n"));
		}
		else
		{
			aProperties.Append(_L("Not System File\n"));
		}

		if (entry.IsArchive())
		{
			aProperties.Append(_L("Archive File\n"));
		}
		else
		{
			aProperties.Append(_L("Not Archive File\n"));
		}
	}
}

